package com.inforich.app.api.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.inforich.app.api.dto.TransportCompanyDTO;
import com.inforich.app.api.dto.TransportCompanyPermitDTO;
import com.inforich.app.api.service.IAppTransportCompanyService;
import com.inforich.app.component.LoginUserApp;
import com.inforich.appManage.domain.Contact;
import com.inforich.appManage.service.IContactService;
import com.inforich.common.core.domain.AjaxResult;
import com.inforich.company.domain.TransportCompany;
import com.inforich.company.service.ITransportCompanyService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @description: some desc
 * @author: HJM
 * @date: 2022/10/31
 */
@Service
public class AppTransportCompanyServiceImpl implements IAppTransportCompanyService {

  @Autowired
  private IContactService contactService;
  @Autowired
  private ITransportCompanyService transportCompanyService;

  @Override
  @Transactional(rollbackFor = Exception.class)
  public AjaxResult addCompany(LoginUserApp loginUserApp, TransportCompanyDTO dto) {
    TransportCompany transportCompany = getMyCompany(loginUserApp);
    if (transportCompany != null) {
      return AjaxResult.error("公司信息已存在，不可重复添加");
    }
    transportCompany = new TransportCompany();
    // 判断公司名称是否有重复
    int countName = transportCompanyService.count(new LambdaQueryWrapper<TransportCompany>()
        .eq(TransportCompany::getName, dto.getName()));
    if (countName > 0) {
      return AjaxResult.error("该公司名称已被认证");
    }
    // 判断统一社会信用码是否重复
    int countCode = transportCompanyService.count(new LambdaQueryWrapper<TransportCompany>()
        .eq(TransportCompany::getSocialCreditCode, dto.getSocialCreditCode()));
    if (countCode > 0) {
      return AjaxResult.error("该统一社会信用码已被认证");
    }
    BeanUtils.copyProperties(dto, transportCompany);
    transportCompanyService.insertTransportCompany(transportCompany);
    // 添加公司负责人账户关系
    contactService.addCompany(loginUserApp.getId(), transportCompany.getId());
    return AjaxResult.success();
  }

  @Override
  public TransportCompany getMyCompany(LoginUserApp loginUserApp) {
    String companyId = contactService.getTransportCompanyIdByAppUserId(loginUserApp.getId());
    if (StrUtil.isBlank(companyId)) {
      return null;
    }
    return transportCompanyService.getTransportCompanyById(companyId);
  }

  @Override
  public AjaxResult updateTransportPermitInfo(LoginUserApp loginUserApp,
      TransportCompanyPermitDTO dto) {
    String companyId = contactService.getTransportCompanyIdByAppUserId(loginUserApp.getId());
    if (StrUtil.isBlank(companyId)) {
      return AjaxResult.error("请先完善公司信息");
    }
    transportCompanyService.update(new LambdaUpdateWrapper<TransportCompany>()
        .set(TransportCompany::getTransportPermitStartDate, dto.getTransportPermitStartDate())
        .set(TransportCompany::getTransportPermitEndDate, dto.getTransportPermitEndDate())
        .set(TransportCompany::getTransportPermitImage, dto.getTransportPermitImage())
        .eq(TransportCompany::getId, companyId));
    return AjaxResult.success();
  }
}
